package com.hxit.huaxiamelongarden.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConfig
 * @description 配置 RedisTemplate
 * @Author 12694
 * @Date 2025/1/29 11:12
 * @Version 1.0
 **/
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 设置序列化方式，防止 Redis 存储的数据变成乱码
        template.setKeySerializer(new StringRedisSerializer());  // 序列化键
        template.setValueSerializer(new StringRedisSerializer());  // 序列化值
        template.setHashKeySerializer(new StringRedisSerializer());  // 序列化哈希的键
        template.setHashValueSerializer(new StringRedisSerializer());  // 序列化哈希的值

        return template;
    }
}
